package com.estar.integaration.Controller.paymentPlanController;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estar.integaration.Reader.ReceiptComeLedgerReader;
import com.estar.integaration.Service.paymentPlanService.IPaymentPlanHistoryService;
import com.estar.integaration.Service.paymentPlanService.IPurchaseOrderDetailService;
import com.estar.integaration.Service.paymentPlanService.IReceiptComeLedgerService;
import com.estar.integaration.Util.FileUtil;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.NoSQL.Res;
import com.estar.integaration.entity.paymentPlanEntity.PaymentPlanHistory;
import com.estar.integaration.entity.paymentPlanEntity.ReceiptComeLedger;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Timestamp;
import java.util.List;
import java.util.Vector;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YJL
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/receipt-come-ledger")
public class ReceiptComeLedgerController {
  @Value("${spring.profiles.active}")
  private String mEnvironment;

  @Autowired
  private IReceiptComeLedgerService receiptComeLedgerService;

  @Autowired
  private IPurchaseOrderDetailService purchaseOrderDetailService;

  @Autowired
  private IPaymentPlanHistoryService paymentPlanHistoryService;

  @PostMapping("/import-receipt-come-ledger")
  @ResponseBody
  public Res importExcel(HttpServletRequest request) {
    String folderAddress = mEnvironment.equals("dev") ? "D:\\SCM_file\\paymentPlan\\receiptComeLedger" : "/root/excel/paymentPlan/receiptComeLedger";
//    String folderAddress = "/root/excel/inventoryFile/inventoryAge";
    //删除原文件夹全部文件
    Util.deleteAllFile(new File(folderAddress));
    //添加进去新文件
    MultipartHttpServletRequest params = (MultipartHttpServletRequest) request;
    List<MultipartFile> multipartFiles = params.getFiles("files");
    Vector<String> multipartFilePath = new Vector<>();//上传文件的地址
    for (MultipartFile file : multipartFiles) {
      //文件写入本地
      String address = Util.writePaymentFile2Local(file,folderAddress);
      multipartFilePath.add(address);
      insertHistory(file.getOriginalFilename());
    }
    return new Res("20000",new Gson().toJson(paymentPlanHistoryService.getHistoryByType("到票台账")));//上传成功
  }


  private List<ReceiptComeLedger> readReceipt(String folderAddress){
    List<String> fileList = FileUtil.getFilesinFolder(folderAddress);
    ReceiptComeLedgerReader reader = new ReceiptComeLedgerReader(fileList);
    List<ReceiptComeLedger> list = reader.readReceiptComeLedger();
    return list;
  }

  //插入应付单历史
  private void insertHistory(String fileName){
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    PaymentPlanHistory paymentPlanHistory = new PaymentPlanHistory(Util.getUUID(),fileName,timestamp,"到票台账");
    paymentPlanHistoryService.save(paymentPlanHistory);
  }

}
